package com.service.socket.support;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

@Slf4j
@Component
public class TradingSpotCache {

    @Autowired
    protected RedisTemplate<String, String> redisTemplate;

    public static String depth_key = "spot:handicap:";

    public static String kline_pre_key = "spot:kline:";

    public static String statistic_key = "statistic:";

    public static String interval = ":";

    public void cacheDepth(String symbol, String value) {
        setNoExpire(buildDepthKey(symbol), value);
    }

    public String getDepth(String symbol) {
        return get(buildDepthKey(symbol));
    }

    public String buildDepthKey(String symbol) {
        symbol = symbol.replace("/", "_");
        return depth_key + symbol;
    }

    public void cacheKline(String symbol, Integer period, String value) {
        setNoExpire(buildKlineKey(symbol, period), value);
    }

    public void cacheKlineStatistic(String symbol, String value) {
        setNoExpire(buildKlineStatisticKey(symbol), value);
    }

    public String getKlineStatistic(String symbol) {
        return get(buildKlineStatisticKey(symbol));
    }

    public String getKline(String symbol, Integer period) {
        return get(buildKlineKey(symbol, period));
    }

    public String buildKlineKey(String symbol, Integer period) {
        symbol = symbol.replace("/", "_");
        return kline_pre_key + symbol + interval + period;
    }

    public String buildKlineStatisticKey(String symbol) {
        symbol = symbol.replace("/", "_");
        return kline_pre_key + statistic_key + symbol;
    }

    /**
     * 新增不过期的数据
     */
    public String setNoExpire(String key, String value) {
        redisTemplate.opsForValue().set(key, value);
        return "OK";
    }

    /**
     * 读取
     */
    public String get(String key) {
        return redisTemplate.opsForValue().get(key);
    }


}
